package com.xci.platform.auth.controller;

import com.xci.platform.annotation.Authorize;
import com.xci.platform.auth.entity.SequenceEntity;
import com.xci.platform.auth.service.SequenceService;
import com.xci.platform.core.BoolMessage;
import com.xci.platform.core.JsonGrid;
import com.xci.platform.helper.ExcelHelper;
import com.xci.platform.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 系统序列控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/auth/sequence")
public class SequenceController extends AuthBaseController {

    /**
     * 系统序列服务
     */
    @Resource
    protected SequenceService sequenceService;

    //region 页面视图

    /**
     * 系统序列首页
     */
    @GetMapping
    @Authorize(code = "SystemSequence")
    public String index() {
        return "auth/sequence/index";
    }

    /**
     * 系统序列新建页面
     */
    @GetMapping("/create")
    @Authorize(code = "SystemSequence.Create")
    public String create(ModelMap map) {
        createMark(map);
        SequenceEntity entity = new SequenceEntity();
        entity.setId(StringHelper.guid());
        entity.setSeqStart(1);
        entity.setSeqStep(1);
        map.put("entity", entity);
        return "auth/sequence/edit";
    }

    /**
     * 系统序列编辑页面
     */
    @GetMapping("/edit")
    @Authorize(code = "SystemSequence.Edit")
    public String edit(String id, ModelMap map) {
        SequenceEntity entity = sequenceService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/sequence/edit";
    }

    /**
     * 系统序列详细页面
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        SequenceEntity entity = sequenceService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/sequence/details";
    }

    //endregion

    //region 数据处理

    /**
     * 系统序列表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "SystemSequence.Search")
    public JsonGrid grid(@RequestParam Map params) {
        return BoolMessage.grid(sequenceService.queryPage(params));
    }

    /**
     * 保存新增系统序列
     */
    @ResponseBody
    @PostMapping("/create-save")
    @Authorize(code = "SystemSequence.Create")
    public BoolMessage createSave(@ModelAttribute SequenceEntity entity ) {
        return sequenceService.save(entity, true);
    }

    /**
     * 保存编辑系统序列
     */
    @ResponseBody
    @PostMapping("/edit-save")
    @Authorize(code = "SystemSequence.Edit")
    public BoolMessage editSave(@ModelAttribute SequenceEntity entity ) {
        return sequenceService.save(entity, false);
    }

    /**
     * 删除系统序列
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "SystemSequence.Delete")
    public BoolMessage delete(String ids) {
        for (String id : StringHelper.splitToArray(ids)) {
            sequenceService.delete(id);
        }
        return BoolMessage.True;
    }

    /**
     * 导出系统序列
     */
    @GetMapping("/export")
    @Authorize(code = "SystemSequence.Export")
    public void export(@RequestParam Map params, HttpServletResponse response) throws IOException {
        ExcelHelper.exportWeb(response, sequenceService.query(params), SequenceEntity.class, "系统序列");
    }

    //endregion
}
